package com.yunze.apiCommon.upstreamAPI.TuJu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunze.apiCommon.utils.HttpUtil;
import com.yunze.apiCommon.utils.MD5Util;

import java.util.*;

public class TuJuApi {
    protected String server_Ip = "http://rabchaser.com";//兔罝Api地址
    // user_id
    protected String appid = null;
    // 秘钥密码
    protected String secret_key = null;
    //请求头部参数
    protected Map<String, String> headers = null;

    //构造赋值
    public TuJuApi(Map<String, Object> init_map) {
        appid = init_map.get("cd_username").toString();
        secret_key = init_map.get("cd_pwd").toString();
        //时间戳
        String timestamp = System.currentTimeMillis() + "";
        UUID uuid = UUID.randomUUID();
        String nonceStr = uuid.toString().replace("-", "").substring(0, 16);
        //生成签名的随机字符串
        headers = new HashMap<>();
        headers.put("appId", appid);
        headers.put("Content-Type", "application/json");
        headers.put("timestamp", timestamp);
        headers.put("nonceStr", nonceStr);
        String stringA = "appId=" + appid + "&nonceStr=" + nonceStr + "&timestamp=" + timestamp;
        //String stringA = "appId=orJM1M7m000eJ7D&nonceStr="+nonceStr+"&timestamp="+timestamp;
        String stringSignTemp = stringA + "&key=" + secret_key;
        String sign = MD5Util.MD5Encode(stringSignTemp).toUpperCase();
        headers.put("sign", sign);
    }


    public Map<String, Object> cardDetail(String iccid) {
        try {
            String api_url = server_Ip + "/iotapi/v1/simCards/getSimCardDetail";
            //请求参数 Json
            JSONObject jsonObject = new JSONObject();
            List<String> objects = new ArrayList<>();
            objects.add(iccid);
            jsonObject.put("iccid", objects);
            System.out.println(api_url);
            System.out.println(iccid);
            System.out.println(headers);
            System.out.println(jsonObject);
            String res = HttpUtil.post(api_url, JSON.toJSONString(jsonObject), headers);
            System.out.println(res);
            Map<String, Object> map = JSONObject.parseObject(res);
            return map;
        } catch (Exception e) {
            System.out.println(e);
        }
        return null;
    }

    public Map<String, Object> getRecentFlow(String iccid) {

        try {
            String api_url = server_Ip + "/iotapi/v1/simCards/getSimCardInfo";
            //请求参数 Json
            Map<String, Object> params = new HashMap<>();
            JSONObject jsonObject = new JSONObject();
            List<String> objects = new ArrayList<>();
            objects.add(iccid);
            params.put("iccid", objects);
            String s = JSON.toJSONString(params);
            System.out.println(s);
            System.out.println(params.toString());
            System.out.println(api_url);
            System.out.println(params);
            System.out.println(headers);
            String res = HttpUtil.post(api_url, JSON.toJSONString(params), headers);
            Map<String, Object> map = JSONObject.parseObject(res);
            return map;
        } catch (Exception e) {
            System.out.println(e);
        }
        return null;
    }

    /*public Map<String, Object> cardActiveTime(String cardId, String cardType) {
        try {
            String api_url = server_Ip + "/open-api/query/cardInfo";
            //请求参数 Json
            Map<String, Object> params = new HashMap<>();
            params.put("cardId", cardId);
            params.put("cardType", cardType);
            //JSONObject jsonObject = JSONObject.parseObject();
            String s = JSONObject.toJSONString(params);
            System.out.println(params.toString());
            System.out.println(api_url);
            System.out.println(params);
            System.out.println(headers);

            String res = HttpUtil.postCommon(api_url, s, headers, null);
            Map<String, Object> map = JSONObject.parseObject(res);
            return map;
        } catch (Exception e) {
            System.out.println(e);
        }
        return null;

    }*/

    public static void main(String[] args) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("appid", "04007J0VIoeeE04");
        map.put("secret_key", "n#1l$5K!W5Cam#5H1#y5915g#rj#gu");
        TuJuApi qr = new TuJuApi(map);
        //System.out.println(qr.cardDetail("898604991023C0364400"));
        System.out.println(qr.getRecentFlow("898604991023C0364400"));

      /*  try {
            String res = HttpUtil.doPost(api_url, params, headers);
            JSONObject jsonObject = JSONObject.parseObject(res);
            System.out.println(headers);
            System.out.println(jsonObject);
            System.out.println(res);
        }catch (Exception e){
        }*/

    }
}
